<template>
	<view>
		<cu-custom bgColor="bg-white" :isBack="true">
			<block slot="backText"></block>
			<block slot="content">提现</block>
		</cu-custom>

		<view class="qui-cells solid-top">
			<view class="qui-cell" @tap="showModal" data-target="Modal">
				<view class="cell-hd fs26 fw600">
					提现至
				</view>
				<view class="cell-bd fs26 fw600 tr" v-text="currentBank.Title">
					
				</view>
				<view class="arrow-r"></view>
			</view>
		</view>
		<view class="qui-cells mt10">
			<view class="qui-cell border_none">
				<view class="cell-hd fs26 fw600">
					提现金额
				</view>
			</view>
			<view class="qui-cell border_none pt0">
				<view class="flex radius10" style="background-color: #f7f7f7;padding: 20rpx;width: 100%;">
					<view class="fs36 fw600 mr10">￥</view>
					
					<input class="fs38 mt5 ff" placeholder="输入提现金额" v-on:blur="changeTiMoney" v-model="WithdrawalsApply.Money" name="input"></input>
				</view>
			</view>
			<view class="qui-cell pt0" v-if="tag==1">
				<view class="cell-hd fs22 text-gray">
					可提现余额￥<text v-text="UserBalance.LiveMoney"></text>
				</view>
				<view class="cell-bd fs24 text-orange tr" @tap="tiAll">
					全部
				</view>
			</view>
			<view class="qui-cell pt0" v-if="tag==2">
				<view>
					<image :src="imgurl+'/icon_notice01.png'" class="img24 mr10" v-if="errtips.length>0"></image>
					<text class="fs22 text-red" v-text="errtips"></text>
				</view>
			</view>
			<view class="qui-cell pt0" v-if="tag==3">
				<view class="fs22 text-gray">
					额外扣除￥<text v-text="kouMoney"></text>服务费(费率<text v-text="FeeBili"></text>%）
				</view>
			</view>
		</view>
		<view class="cu-modal" :class="modalName=='Modal'?'show':''">
			<view class="cu-dialog" style="width: 600rpx;">
				<view class="cu-bar bg-white">
					<view class="fs30 fw600 ml30">提现至</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-gray"></text>
					</view>
				</view>
				<view>
					<label class="cu-form-group" v-for="(item,index) in cards" @tap="activeIndex = index;selectBind(item);modalName='no'">
						<view class="flex align-center pt20 pb20">
							<view>
								<image :src="imgurl+bindURL(item.TiMode, item.Name)" class="img74 mr30"></image>
							</view>
							<view style="text-align: left;">
								<view class="fs28 fw600">{{item.Name}}</view>
								<view class="fs22 text-muted">{{ item.TiMode == 4 ? item.Account.substring(item.Account.length - 4,item.Account.length) : item.Account}}</view>
							</view>
						</view>
						<view v-show="activeIndex == index">
							<image :src="imgurl+'/icon_check.png'" class="img36"></image>
						</view>
					</label>
					<navigator class="cu-form-group padding" v-on:tap="modalName='no'"  url="/pages/my/setting/bank">
						<view class="fs28 fw600" >添加银行卡</view>
						<view>
							<image :src="imgurl+'/icon_add01.png'" class="img30"></image>
						</view>
					</navigator>
				</view>


			</view>
		</view>

		<view  class="uni-btns text-bold mt30" v-on:tap="apply">
			<button class="uni-btn-main radio uni-btn fs30">确定</button>
		</view>
		<navigator class="fs24 tc" style="color: #008aff;">
			提现协议
		</navigator>
	</view>
</template>

<script>
	import base from 'utils/base.js';
	import tools from 'utils/tools.js';
	export default {
		data() {
			return {
				tag:1,
				WithdrawalsApply:{Money:0,TiMode:0,BankId:0},
				WithdrawalsSet:{},
				activeIndex: 0,
				modalName: null,
				imgurl: this.$imgurl,
				FeeBili:0,
				kouMoney:0,
				cards:[],
				currentBank:{Title:"绑定银行卡"},
				UserBalance:{},
				errtips:"",
				items: [{
						url: '/icon_pay01.png',
						text: '微信',
						num: '',
					},
					{
						url: '/icon_pay02.png',
						text: '支付宝',
						num: '林晓然(18952891980)',
					},
					{
						url: '/icon_pay03.png',
						text: '中国银行',
						num: '林晓然(3089)',
					},

				],

			}
		},
		mounted(){
		   let time = 0;
		   	let globalData = getApp().globalData;
		   let current = this;
		   let si = setInterval(function() {
		   	time = time + 1;
		   	if (globalData.ticket) {
		   		current.GetWithdrawalsSet();
		   		current.GetUserBalance();
		   		clearInterval(si);
		   	}
		   	if (time > 40) clearInterval(si);
		   }, 200);
			
		},
		onShow:function () {
			let time = 0;
				let globalData = getApp().globalData;
			let current = this;
			let si = setInterval(function() {
				time = time + 1;
				if (globalData.ticket) {
					current.GetUserBanks();
					clearInterval(si);
				}
				if (time > 40) clearInterval(si);
			}, 200);
			
			
		},
		methods: {
			bindURL:function(timode,bankname){
				if(timode==1) return '/icon_pay02.png';
				else if(timode==2) return '/icon_pay01.png';
				else if(timode==3) return '/icon_pay01.png';
				else if(timode==6) return '/icon_pay01.png';
				else if(timode==4) {
					if (bankname == '工商银行') return 'icon_bank_22.png';
					if (bankname == '建设银行') return 'icon_bank_55.png';
					if (bankname == '中国银行') return 'icon_bank_77.png';
					if (bankname == '农业银行') return 'icon_bank_66.png';
					if (bankname == '招商银行') return 'icon_bank_11.png';
				};
			},
			showModal(e) {
				this.modalName = 'Modal'
			},
			hideModal(e) {
				this.modalName = null
			},
			 selectBind:function(item)
			{
					let currentPage = this;
					currentPage.currentBank=item;
					currentPage.WithdrawalsApply.TiMode=currentPage.currentBank.TiMode;
					currentPage.WithdrawalsApply.BankId=currentPage.currentBank.BankId;
			},
			changeTiMoney:function(obj){
				
				var money=obj.detail.value;
					let currentPage = this;
					currentPage.errtips="";
					if(!tools.IsMoney(money)) {
						currentPage.recover();
						currentPage.errtips="请准确输入金额";
							currentPage.tag=2;
						return;
					}
				
				currentPage.Caculate(parseFloat(money));
				
				
			},
			apply:function(){
					let currentPage = this;
					
					if(!currentPage.WithdrawalsSet.IsOpen)
					{
						currentPage.errtips="提现没有开启";
						currentPage.tag=2;
						return;
					}
					currentPage.WithdrawalsApply.Money=parseFloat(currentPage.WithdrawalsApply.Money);
					if(currentPage.WithdrawalsApply.Money>currentPage.WithdrawalsSet.OneTiMaxMoney && currentPage.WithdrawalsSet.OneTiMaxMoney>0)
					{
					currentPage.tag=2;
						currentPage.errtips="输入金额超过可提现最高额度(￥"+currentPage.WithdrawalsSet.OneTiMaxMoney+")";
						return;
					}
					
					if(currentPage.WithdrawalsApply.Money+parseFloat(currentPage.kouMoney)>currentPage.UserBalance.LiveMoney)
					{
						currentPage.errtips="输入金额超过可提现余额(￥"+currentPage.UserBalance.LiveMoney+")";
					currentPage.tag=2;
						return;
					}
					
					if(currentPage.WithdrawalsApply.TiMode==3 && (currentPage.WithdrawalsApply.Money>200 || currentPage.WithdrawalsApply.Money<1))
						
					{
						currentPage.errtips="微信红包提现金额为最低1元 最高200元";
							currentPage.tag=2;				
						return;
					}
					if(currentPage.WithdrawalsApply.TiMode==6 && ( currentPage.WithdrawalsApply.Money<0.3))
						
					{
						currentPage.errtips="微信企业打款提现金额为最低0.3元";
							currentPage.tag=2;				
						return;
					}
					
					let params = {
						type: 'POST',
						url: '/api/Withdrawals/ApplyWithdrawals',
						data: currentPage.WithdrawalsApply,
						sCallback: function(response) {
							if (response.IsSuccess) {
							
								uni.navigateBack({
								    delta: 1
								});
							} else {
								tools.showToast(response.Msg);
								currentPage.tag=1;
							}
						}
					};
					base.request(params);
			},
			tiAll:function(){
				let currentPage = this;
				var livemoney=currentPage.UserBalance.LiveMoney;
				let timoney=(livemoney/(1+currentPage.FeeBili*1.0/ 100)).toFixed(2);
				let koumoney=livemoney-timoney;
				currentPage.WithdrawalsApply.Money=timoney;
				currentPage.kouMoney=koumoney.toFixed(2);
				if(currentPage.kouMoney>0) currentPage.tag=3;
			},
			Caculate:function(timoney){
					let currentPage = this;
			let	kouMoney = 0;
			if(currentPage.WithdrawalsSet.IsOpenFee==1)  kouMoney=timoney * currentPage.FeeBili / 100;
			if(timoney>currentPage.WithdrawalsSet.OneTiMaxMoney && currentPage.WithdrawalsSet.OneTiMaxMoney>0)
			{
				currentPage.recover();
				currentPage.errtips="输入金额超过可提现最高额度(￥"+currentPage.WithdrawalsSet.OneTiMaxMoney+")";
					currentPage.tag=2;
				return;
			}
			
			if(timoney+kouMoney>currentPage.UserBalance.LiveMoney)
			{
				currentPage.errtips="输入金额超过可提现余额(￥"+currentPage.UserBalance.LiveMoney+")";
				currentPage.tiAll();
					currentPage.tag=2;
				return;
			}
			currentPage.kouMoney=kouMoney.toFixed(2);
					currentPage.tag=3;
			},
			recover:function(){
					let currentPage = this;
					currentPage.kouMoney=0;
					currentPage.WithdrawalsApply.Money=0;
			},
			GetUserBalance: function() {
				let currentPage = this;
				let globalData = getApp().globalData;
				let params = {
					type: 'GET',
					url: '/api/UserInfo/GetUserBalance',
					data: {typeId:6},
					sCallback: function(response) {
						if (response.IsSuccess) {
							let userBalance = response.Data;
							currentPage.UserBalance = userBalance;
			
						} else {
							uni.showToast({
								title: response.Msg
							});
						}
					}
				};
				base.request(params);
			},
			GetWithdrawalsSet: function() {
				let currentPage = this;
				let globalData = getApp().globalData;
				let params = {
					type: 'GET',
					url: '/api/Withdrawals/GetWithdrawalsSet',
					data: {},
					sCallback: function(response) {
						if (response.IsSuccess) {
							let WithdrawalsSet = response.Data;
						currentPage.WithdrawalsSet=WithdrawalsSet;
						if(WithdrawalsSet.IsOpenFee==1) currentPage.FeeBili=currentPage.WithdrawalsSet.FeeBili;
			
						} else {
							uni.showToast({
								title: response.Msg
							});
						}
					}
				};
				base.request(params);
			},
			GetUserBanks: function() {
				let currentPage = this;
				let globalData = getApp().globalData;
				let params = {
						hideLoading:true,
					type: 'GET',
					url: '/api/Withdrawals/GetUserBanks',
					data: {},
					sCallback: function(response) {
						if (response.IsSuccess) {
							let cards = response.Data;
						currentPage.cards=cards;
						if(cards!=null && cards.length>0) {
							currentPage.currentBank=cards[0];
							currentPage.WithdrawalsApply.TiMode=currentPage.currentBank.TiMode;
							currentPage.WithdrawalsApply.BankId=currentPage.currentBank.BankId;
						}
			
						} else {
							uni.showToast({
								title: response.Msg
							});
						}
					}
				};
				base.request(params);
			},
			radioChange: function(evt) {
				for (let i = 0; i < this.items.length; i++) {
					if (this.items[i].value === evt.target.value) {
						this.current = i;
						break;
					}
				}
			}
		},


	}
</script>

<style lang="scss">
	.uni-btn-main {
		background-color: #ff5e1f !important;
	}

	.img24 {
		width: 24rpx;
		height: 24rpx;
		position: relative;
		top: 5rpx;
	}

	.img74 {
		width: 74rpx;
		height: 74rpx;
	}

	.img30 {
		width: 30rpx;
		height: 30rpx;
	}

	.img36 {
		width: 36rpx;
		height: 36rpx;
	}
</style>
